package com.linkhub.oms.data.sync.service.impl;

import com.linkhub.oms.common.enums.mp.DataSyncTypeEnum;
import com.linkhub.oms.common.util.QtBeanUtil;
import com.linkhub.oms.common.util.QtOptionalUtil;
import com.linkhub.oms.dao.mpservice.IDataSyncRecordService;
import com.linkhub.oms.data.sync.api.shopify.domain.dto.DataSyncRecordDTO;
import com.linkhub.oms.data.sync.service.DataSyncRecordBizService;
import com.linkhub.oms.entity.DataSyncRecordEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Description: DataSyncRecordBizService
 * @Author: echo
 * @Date: 2024/3/23 10:04
 * @Version: 1.0
 */
@Slf4j
@Service
public class DataSyncRecordBizServiceImpl implements DataSyncRecordBizService {

    @Resource
    private IDataSyncRecordService dataSyncRecordService;

    @Override
    public String getLatestSyncUpdateTime(String sysShopId, DataSyncTypeEnum dataSyncType) {
        DataSyncRecordEntity record = dataSyncRecordService.queryLatestSyncRecord(sysShopId, dataSyncType);
        return QtOptionalUtil.of(record)
                .map(DataSyncRecordEntity::getMaxUpdatedTime)
                .orElse(StringUtils.EMPTY);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRecord(DataSyncRecordDTO dataSyncRecordDTO) {
        DataSyncRecordEntity record = new DataSyncRecordEntity();
        QtBeanUtil.copyProperties(dataSyncRecordDTO, record);
        dataSyncRecordService.save(record);
    }
}
